package DAO;

import Logica.Owner;
import Logica.Pen;
import Logica.Staff;

import Logica.Supplies;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Types;

import java.util.ArrayList;
import java.util.Vector;

public class ClinicDao {
  public ClinicDao() {
  }


  public boolean creaClinica(String manager, int ManNum) {

    int ClinicNumber = -5;
    Conexion conexion = new Conexion();
    boolean creado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ResultSet rsa = null;
      int mayor = -1;
      ps = conex.prepareStatement("select max (clinicNumber)  from clinic");

      rsa = ps.executeQuery();
      if (rsa != null) {
        while (rsa.next()) {
          mayor = rsa.getInt("clinicNumber");
        }
      } else {
        ClinicNumber = 1;
      }
      ps =
          conex.prepareStatement("  INSERT INTO CLINIC (CLINICNUMBER, MANAGER,MANNUM) VALUES (?, ?,?)");

      ClinicNumber = mayor + 1;
      ps.setInt(1, ClinicNumber);
      ps.setString(2, manager);
      if (ManNum != -1)
        ps.setInt(3, ManNum);
      else
        ps.setNull(3, Types.INTEGER);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);
      creado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return creado;
  }


  public boolean creaClinicaTotal(String manager, int ManNum,
                                  ArrayList<Staff> astaff,
                                  ArrayList<Supplies> asupplies,
                                  ArrayList<Owner> aowner,
                                  ArrayList<Pen> apen) {

    int ClinicNumber = -5;
    Conexion conexion = new Conexion();
    boolean creado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ResultSet rsa = null;
      int mayor = -1;
      ps = conex.prepareStatement("select max (clinicNumber)  from clinic");

      rsa = ps.executeQuery();
      if (rsa != null) {
        while (rsa.next()) {
          mayor = rsa.getInt(1);
        }
      } else {
        ClinicNumber = 1;
      }
      ps =
          conex.prepareStatement("  INSERT INTO CLINIC (CLINICNUMBER, MANAGER,MANNUM) VALUES (?, ?,?)");

      ClinicNumber = mayor + 1;
      ps.setInt(1, ClinicNumber);
      ps.setString(2, manager);
      if (ManNum != -1)
        ps.setInt(3, ManNum);
      else
        ps.setNull(3, Types.INTEGER);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);
      creado = true;
      conex.close();

      for (int i = 0; i < astaff.size(); i++) {
        // astaff[i].creaStaff(parametros);
      }
      for (int i = 0; i < asupplies.size(); i++) {
        // asupplies[i].creaSupplies(parametros);
      }
      for (int i = 0; i < aowner.size(); i++) {
        // aowner[i].creaOwner(parametros);
      }
      for (int i = 0; i < apen.size(); i++) {
        // apen[i].creaPen(parametros);
      }

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return creado;
  }

  public boolean eliminarClinic(int ClinicNumber) {

    Conexion conexion = new Conexion();
    boolean eliminado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ps = conex.prepareStatement("DELETE FROM Clinic where clinicNumber = ?");

      ps.setInt(1, ClinicNumber);


      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que borro fueron" + numeroFilas);
      eliminado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return eliminado;
  }


  public boolean modificaClinicManager(int ClinicNumber, String Manager,
                                       int ManNum) {
    Conexion conexion = new Conexion();
    Connection conex;
    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;

      ps =
          conex.prepareStatement("UPDATE clinic set Manager = ?, MANNUM = ? where clinicNumber = ?");

      ps.setString(1, Manager);
      ps.setInt(2, ManNum);
      ps.setInt(3, ClinicNumber);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que actualizo fueron" +
                         numeroFilas);
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
      return false;
    }
    return true;
  }


  public static Vector<Vector<String>> Consultar_Clinic() {
    Vector<Vector<String>> matriz = new Vector<Vector<String>>();
    Vector<String> fila = new Vector<String>();
    Vector<String> id = new Vector<String>();
    Conexion conexion = new Conexion();
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ResultSet rsa = null;
      String nombre = new String("");
      ps = conex.prepareStatement("select clinicNumber\n" +
            "from clinic");
      rsa = ps.executeQuery();

      if (rsa != null) {
        while (rsa.next()) {
          fila.add(rsa.getString("clinicNumber"));
          id.add(rsa.getString("clinicNumber"));
        }
      }
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    matriz.add(id);
    matriz.add(fila);
    return matriz;
  }
  
  
  public  Vector<String> Consultar_Clinica() {
    Vector<String> matriz = new Vector<String>();
    Conexion conexion = new Conexion();
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ResultSet rsa = null;
      String nombre = new String("");
      ps = conex.prepareStatement("select clinicNumber\n" +
            "from clinic");
      rsa = ps.executeQuery();

      if (rsa != null) {
        while (rsa.next()) {
          matriz.add(rsa.getString("clinicNumber"));
           }
      }
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
   
    return matriz;
  }
}
